package com.dayaokeji.rhythmschoolstudent.client.home.course.ranking;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;

import com.dayaokeji.rhythmschoolstudent.R;
import com.dayaokeji.rhythmschoolstudent.client.common.base.common.BaseActivity;
import com.dayaokeji.rhythmschoolstudent.client.home.course.ranking.adapter.FindCourseTemplateAdapter;
import com.dayaokeji.rhythmschoolstudent.client.home.course.services.CourseService;
import com.dayaokeji.rhythmschoolstudent.utils.CourseUtils;
import com.dayaokeji.rhythmschoolstudent.utils.ItemDecorationUtil;
import com.dayaokeji.rhythmschoolstudent.utils.UserUtils;
import com.dayaokeji.rhythmschoolstudent.wiget.WrapLinearLayoutManager;
import com.dayaokeji.server_api.domain.CourseTemplate;
import com.dayaokeji.server_api.domain.UserInfo;

import butterknife.BindView;

/**
 * full template course
 */
public class FindCourseTemplateAllActivity extends BaseActivity {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.rv_course_template_all)
    RecyclerView rvCourseTemplateAll;

    private final FindCourseTemplateAdapter findCourseTemplateAdapter = new FindCourseTemplateAdapter();

    public static void navigation(Context context) {
        if (context == null) return;
        Intent intent = new Intent(context, FindCourseTemplateAllActivity.class);
        ActivityCompat.startActivity(context, intent, null);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_course_all);
        setSupportActionBar(toolbar);
        init();
    }


    private void init() {
        setupView();
        setupViewData();
        setOnListener();
    }

    private void setupView() {
        rvCourseTemplateAll.setLayoutManager(new WrapLinearLayoutManager(this));
        rvCourseTemplateAll.addItemDecoration(ItemDecorationUtil.horizontalDividerItemDecoration(this));
        rvCourseTemplateAll.setAdapter(findCourseTemplateAdapter);
    }

    private void setupViewData() {
        reqTemplateCourses();
    }

    private void setOnListener() {
        findCourseTemplateAdapter.setOnItemClickListener((adapter, view, position) -> {
            if (adapter.getData().size() < position) return;
            CourseTemplate courseTemplate = (CourseTemplate) adapter.getData().get(position);
            CourseAllListActivity.navigation(FindCourseTemplateAllActivity.this, courseTemplate);
        });
    }


    private void reqTemplateCourses() {
        UserInfo userInfo = UserUtils.getUserInfo();
        String[] currentTermDate = CourseUtils.calCurrentTermDate();
        if (userInfo == null) return;
        if (userInfo.getId() == null) return;
        addDisposable(CourseService.INSTANCE.findTemplateCourseAll(currentTermDate, userInfo.getId()).subscribe(response -> {
            if (!isFinishing()) {
                findCourseTemplateAdapter.setNewData(response);
            }
        }));
    }

}
